package com.blb.exam.dao.impl;

import com.blb.exam.common.entity.Quetion;
import com.blb.exam.common.util.DBUtils;
import com.blb.exam.dao.QuetionDAO;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 考试题集DAO实现
 */
@Repository
public class QuetionDAOImpl implements QuetionDAO {

    @Override
    public Quetion selectById(Long id) {
        List<Quetion> quetions = DBUtils.executeQuery(Quetion.class, "select q.*,s.title from t_quetion q join t_quetion_set s on q.quetion_set_id = s.id where q.id = ?", id);
        if(quetions != null && !quetions.isEmpty()){
            return quetions.get(0);
        }
        return null;
    }

    @Override
    public int insert(Quetion quetion) {
        int rows = DBUtils.executeUpdate("insert into t_quetion(content,answer,option_a,option_b,option_c,option_d,quetion_set_id,difficulty,image) values(?,?,?,?,?,?,?,?,?)",
                quetion.getContent(),quetion.getAnswer(),quetion.getOptionA(),quetion.getOptionB(),quetion.getOptionC(),quetion.getOptionD(),quetion.getQuetionSetId(),quetion.getDifficulty(),quetion.getImage());
        return rows;
    }

    @Override
    public int update(Quetion quetion) {
        int rows = DBUtils.executeUpdate("update t_quetion set content=?,answer=?,option_a=?,option_b=?,option_c=?,option_d=?,quetion_set_id=?,difficulty=?,image=? where id=?",
                quetion.getContent(),quetion.getAnswer(),quetion.getOptionA(),quetion.getOptionB(),quetion.getOptionC(),quetion.getOptionD(),quetion.getQuetionSetId(),quetion.getDifficulty(), quetion.getImage(),quetion.getId());
        return rows;
    }

    @Override
    public int deleteById(int stuId) {
        int rows = DBUtils.executeUpdate("delete from t_quetion where id=?",stuId);
        return rows;
    }

    @Override
    public List<Quetion> selectLimitLikeContent(String title, int start, int length) {
        return DBUtils.executeQuery(Quetion.class,"select q.*,s.title from t_quetion q join t_quetion_set s on q.quetion_set_id = s.id where content like ? limit ?,?",
                "%" + title + "%",start,length);
    }

    @Override
    public int selectCountLikeContent(String title) {
        return DBUtils.executeCount("select count(*) from t_quetion where content like ?","%" + title + "%");
    }

}
